package com.example.demo.common;


import java.util.List;
import java.util.Optional;

/**
 * Created by cqh on 17-9-5.
 */
public class BasePageResponse<T> extends BaseResponse {

    /**
     * 当前页
     */
    private Integer currentPage = 1;

    /**
     * 一页大小
     */
    private Integer pageSize = 10;

    /**
     * 总行数
     */
    private Integer totalLine = 0;

    /**
     * 总页数
     */
    private Integer totalPage;

    /**
     * 返回的bean
     */
    private List<T> data;


    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalLine() {
        return totalLine;
    }

    public void setTotalLine(Integer totalLine) {
        /***
         * 求出总页数
         */
        this.totalLine = totalLine;
        Optional.ofNullable(this.totalLine).ifPresent(totalLine2 ->
                this.totalPage = (int) Math.ceil(totalLine * 1.0 / pageSize)
        );
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * 分页返回参数
     *
     * @param currentPage 当前页
     * @param pageSize    一页大小
     * @param totalLine   总行数
     * @param data        列表数据
     * @param <T>         泛型类
     * @return 分页对象
     */
    public static <T> BasePageResponse<T> getData(Integer currentPage,
                                                  Integer pageSize, Integer totalLine, List<T> data) {
        BasePageResponse<T> responsePage = new BasePageResponse<T>();
        responsePage.setCurrentPage(currentPage);
        responsePage.setPageSize(pageSize);
        responsePage.setTotalLine(totalLine);
        responsePage.setData(data);
        return responsePage;
    }
}
